<?php 

//-------------- 
// IPv4 class 
class ipv4 
{ 
var $address; 
var $netbits; 

//-------------- 
// Create new class 
function ipv4($address,$netbits) 
{ 
$this->address = $address; 
$this->netbits = $netbits; 
} 

//-------------- 
// Return the IP address 
function address() { return ($this->address); } 

//-------------- 
// Return the netbits 
function netbits() { return ($this->netbits); } 

//-------------- 
// Return the netmask 
function netmask() 
{ 
return (long2ip(ip2long("255.255.255.255") 
<< (32-$this->netbits))); 
} 

//-------------- 
// Return the network that the address sits in 
function network() 
{ 
return (long2ip((ip2long($this->address)) 
& (ip2long($this->netmask())))); 
} 

//-------------- 
// Return the broadcast that the address sits in 
function broadcast() 
{ 
return (long2ip(ip2long($this->network()) 
| (~(ip2long($this->netmask()))))); 
} 

//-------------- 
// Return the inverse mask of the netmask 
function inverse() 
{ 
return (long2ip(~(ip2long("255.255.255.255") 
<< (32-$this->netbits)))); 
} 

}
/*
$ip = new ipv4("192.168.2.1",24); 
print  $ip->address()."\n"; 
print  $ip->netbits()."\n"; 
print  $ip->netmask()."\n"; 
print  $ip->inverse()."\n"; 
print  $ip->network()."\n"; 
print  $ip->broadcast()."\n";*/